/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.service.impl;

import me.zhengjie.poemmanager.domain.Favorites;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.poemmanager.service.FavoritesService;
import me.zhengjie.poemmanager.domain.vo.FavoritesQueryCriteria;
import me.zhengjie.poemmanager.mapper.FavoritesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author hwl
* @date 2024-12-07
**/
@Service
@RequiredArgsConstructor
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements FavoritesService {

    private final FavoritesMapper favoritesMapper;

    @Override
    public PageResult<Favorites> queryAll(FavoritesQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(favoritesMapper.findAll(criteria, page));
    }

    @Override
    public List<Favorites> queryAll(FavoritesQueryCriteria criteria){
        return favoritesMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Favorites resources) {
        switch (resources.getObjectType()) {
            case 0:
                favoritesMapper.addPostFavoritesCount(resources.getObjectId());
                break;
            case 1:
                favoritesMapper.addPoemUserFavoritesCount(resources.getObjectId());
                break;
            case 2:
                favoritesMapper.addPoemPoetFavoritesCount(resources.getObjectId());
                break;
            case 3:
                favoritesMapper.addRecitationFavoritesCount(resources.getObjectId());
                break;
            default:
                throw new IllegalArgumentException("无效的收藏类型: " + resources.getObjectType());
        }
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Favorites resources) {
        Favorites favorites = getById(resources.getFavoritesId());
        favorites.copy(resources);
        saveOrUpdate(favorites);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        for(Integer id : ids) {
            Favorites resources = getById(id);
            switch (resources.getObjectType()) {
                case 0:
                    favoritesMapper.subPostFavoritesCount(resources.getObjectId());
                    break;
                case 1:
                    favoritesMapper.subPoemUserFavoritesCount(resources.getObjectId());
                    break;
                case 2:
                    favoritesMapper.subPoemPoetFavoritesCount(resources.getObjectId());
                    break;
                case 3:
                    favoritesMapper.subRecitationFavoritesCount(resources.getObjectId());
                    break;
                default:
                    throw new IllegalArgumentException("无效的收藏类型: " + resources.getObjectType());
            }
        }
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Favorites> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Favorites favorites : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户名", favorites.getUserName());
            map.put("被收藏的id", favorites.getObjectId());
            map.put("评论的地方的类别（0帖子，1诗歌，2朗诵）", favorites.getObjectType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Favorites isBeFavourite(Favorites favorites) {
        return favoritesMapper.isBeFavourite(favorites);
    }
}